export default {
    saveProductInfo(productInfo, spaceId, v) {
        let productXML = this.getProductInfoXML(productInfo);
        let productId = productInfo.id;
        let self = this;
        return new Promise(function (resolve, reject) {
            v.$axios({
                url: '/magicflu/service/s/' + spaceId + '/products/' + productId,
                method: 'put',
                data: productXML
            }).then(function (response) {
                resolve();
            }).catch(function (response) {
            });
        })
    },
    getProductInfoXML(productInfo) {
        let baseInfoXml = ['<productName>' + (productInfo.productName ? productInfo.productName : '') + '</productName>',
        '<productEName>' + (productInfo.productEName ? productInfo.productEName : '') + '</productEName>',
        '<productEdition>' + (productInfo.productEdition ? productInfo.productEdition : '') + '</productEdition>',
        '<version>' + (productInfo.version ? productInfo.version : '') + '</version>',
        '<author>' + (productInfo.author ? productInfo.author : '') + '</author>',
        '<contactWay >' + (productInfo.contactWay ? productInfo.contactWay : '') + '</contactWay >',
        '<description>' + (productInfo.description ? productInfo.description : '') + '</description>',
        '<appId>' + (productInfo.appId ? productInfo.appId : '') + '</appId>',
        '<logo>' + this.getRealImageUrl(productInfo.logo) + '</logo>',
        '<defaultLogo>' + this.getRealImageUrl(productInfo.defaultLogo) + '</defaultLogo>',
        '<aboutLogo>' + this.getRealImageUrl(productInfo.aboutLogo) + '</aboutLogo>',
        '<deskTopIcon>' + this.getRealImageUrl(productInfo.deskTopIcon) + '</deskTopIcon>',
        '<serviceIcon>' + this.getRealImageUrl(productInfo.serviceIcon) + '</serviceIcon>',
        '<serviceStopIcon>' + this.getRealImageUrl(productInfo.serviceStopIcon) + '</serviceStopIcon>',
        '<loginBg>' + this.getRealImageUrl(productInfo.loginBg) + '</loginBg>',
        '<workspaceId>' + productInfo.workspaceId + '</workspaceId>'
        ].join("");
        let installPageXml = '<installPage><![CDATA[<pages>';
        let upgradePageXml = '<upgradePage><![CDATA[<pages>';
        for (let i = 0; i < productInfo.installPage.pages.page.length; i++) {
            let page = productInfo.installPage.pages.page[i];
            installPageXml += this.getPageXml(page);
        }
        installPageXml += '</pages>]]></installPage>';
        for (let i = 0; i < productInfo.upgradePage.pages.page.length; i++) {
            let page = productInfo.upgradePage.pages.page[i];
            upgradePageXml += this.getPageXml(page);
        }
        upgradePageXml += '</pages>]]></upgradePage>';
        return '<entry><id>' + productInfo.id + '</id>' + baseInfoXml + installPageXml + upgradePageXml + '</entry>';
    },
    getPageXml(page) {
        var pageXml = [];
        var backgroundImageURL = page.backgroundImage.url;
        var backgroundImagePath = backgroundImageURL.substring(0, backgroundImageURL.lastIndexOf("/") + 1);
        backgroundImagePath = backgroundImagePath + page.backgroundImage.nsisUseName;
        if (page.pageType == 0) { //0表示带license的安装首页,1表示轮播页,2表示其他只有背景图片的页面
            var licenseURL = page.license.url;
            var licensePath = licenseURL.substring(0, licenseURL.lastIndexOf("/") + 1);
            licensePath = licensePath + page.license.nsisUseName;
            pageXml = ['<page><pageName>' + page.pageName + '</pageName><pageType>' + page.pageType + '</pageType><backgroundImage>',
            '<name>' + page.backgroundImage.name + '</name><url>' + backgroundImagePath + '</url><nsisUseName>' + page.backgroundImage.nsisUseName + '</nsisUseName></backgroundImage>',
            '<license><name>' + page.license.name + '</name><url>' + licensePath + '</url><nsisUseName>' + page.license.nsisUseName + '</nsisUseName></license></page>'
            ].join("");
        } else if (page.pageType == 1) {
            pageXml = ['<page><pageName>' + page.pageName + '</pageName><pageType>' + page.pageType + '</pageType><backgroundImage>',
            '<name>' + page.backgroundImage.name + '</name><url>' + backgroundImagePath + '</url><nsisUseName>' + page.backgroundImage.nsisUseName + '</nsisUseName></backgroundImage>',
            '<stayTime>' + page.stayTime + '</stayTime></page>',
            ].join("");
        } else {
            pageXml = ['<page><pageName>' + page.pageName + '</pageName><pageType>' + page.pageType + '</pageType><backgroundImage>',
            '<name>' + page.backgroundImage.name + '</name><url>' + backgroundImagePath + '</url><nsisUseName>' + page.backgroundImage.nsisUseName + '</nsisUseName></backgroundImage></page>',
            ].join("");
        }
        return pageXml;
    },
    getRealImageUrl(url) {
        //url = '/magicflu/buildpackage/target/00000000-0000-0000-0000-000000000000/625147bf-8448-4f33-88a2-6d846d1e8c99/logo/default/addlogo.png';
        if (!url) return '';
        var urlPath = url.substring(0, url.lastIndexOf("/") + 1);
        var endPosition = url.length;
        if (url.indexOf('?t=') != -1) {
            endPosition = url.indexOf('?t=');
        }
        var fileName = url.substring(url.lastIndexOf("/") + 1, endPosition);
        var realName = fileName.substring(3, fileName.length);
        if (fileName.indexOf("add") != 0) {
            realName = fileName;
        }
        return urlPath + realName;
    },
    getUrlStr(name) {
        let reg = new RegExp("(^|\\?|&)" + name + "=([^&#]*)(#|&|$)", "i");
        if (reg.test(window.location.href)) {
            return unescape(RegExp.$2.replace(/\+/g, " "))
        }
        return undefined;
    },
    myConfirmBox(message, title, callback, vm) {
        const confirm = vm.$confirm({
            title: title,
            text: message,
            dangerouslyUseHTMLString: true,
            showBtn: {
                showConfirm: true,
                showCancel: true
            },
            button: [{
                text: '确定',
                ontap: function () {
                    confirm.close().then(callback);
                }
            },
            {
                text: '取消',
                ontap: function () {

                    confirm.close().then(function (res) {
                    });
                }
            }]
        });
    },
    myAlert(message, title, vm, callback) {
        const confirm = vm.$confirm({
            title: title,
            text: message,
            dangerouslyUseHTMLString: true,
            showBtn: {
                showConfirm: true,
                showCancel: false
            },
            button: [{
                text: '确定',
                ontap: function () {
                    confirm.close().then(callback);
                }
            },
            {
                text: '取消',
                ontap: function () {

                    confirm.close().then(function (res) {
                    });
                }
            }]
        });
    },
    currentBackground(skinName) {
        if (skinName == "green") {
            return "#009B63";
        } else if (skinName == "redleaf") {
            return "#C8224A";
        } else if (skinName == "bluedanube") {
            return "#09357F";
        } else if (skinName == "goldyellow") {
            return "#F9AF00";
        } else if (skinName == "orange") {
            return "#FB541F";
        } else if (skinName == "peacockblue") {
            return "#0098A7";
        } else if (skinName == "skyblue") {
            return "#04A9E2";
        } else if (skinName == "greenlotus") {
            return "#8D23A8";
        } else if (skinName == "irongray") {
            return "#28323A";
        } else if (skinName == "autumnwatergreen") {
            return "#034B3E";
        } else if (skinName == "olivegreen") {
            return "#1C5F1F";
        } else if (skinName == "ginger") {
            return "#807716";
        } else if (skinName == "coccineous") {
            return "#D2637D";
        } else if (skinName == "castaneous") {
            return "#3D2821";
        } else if (skinName == "lavender") {
            return "#512DA7";
        } else if (skinName == "ironstonegray") {
            return "#9E9E9E";
        } else if (skinName == "skinaa") {
            return "#79bd9a";
        } else if (skinName == "skinbb") {
            return "#04D293";
        } else if (skinName == "skincc") {
            return "#e59572";
        } else {
            return "#363B4E";
        }
    }
}